package com.bw.controller;

import com.bw.entity.*;
import com.bw.mapper.StudentMapper;
import com.bw.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.spring.kun.common.ExcelUtils;
import org.spring.kun.common.TokenUtils;
import org.spring.kun.common.ZzyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@Controller
public class StudentController {

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    StudentService studentService;

    @RequestMapping("/list.do")
    public String list(StudentQuery query,Integer pageNum,Model model, HttpServletRequest request){
        // 1. 从 request中获取cookie ，从cookie过滤 出token， 再解密token
        User userFromRequest = TokenUtils.getLoginUserFromRequest(request, User.class);
        model.addAttribute("name",userFromRequest.getName());
        model.addAttribute("nickName",userFromRequest.getNickName());
        model.addAttribute("avatarUrl",userFromRequest.getAvatarUrl());

        //2. 分页查询
        PageHelper.startPage(pageNum == null ? 1:pageNum,3);
        List<StudentRoom> studentRoomList = studentMapper.selectByParam(query);
        PageInfo<StudentRoom> pageInfo = new PageInfo<>(studentRoomList);
        model.addAttribute("pageInfo",pageInfo);

        // 3. 回显条件
        model.addAttribute("query",query);
        return "list";
    }


    @RequestMapping("/excel.do")
    public void excel(StudentQuery query, HttpServletResponse response){
        List<StudentRoom> studentRoomList = studentMapper.selectByParam(query);
        ExcelUtils.exportExcel(studentRoomList,"学生列表","sheet1",response);
    }


    @RequestMapping("/addPage.do")
    public String addPage(Model model){
        //1. 渲染3个下拉框
        List<Dict> studentList = studentMapper.studentList();
        model.addAttribute("studentList",studentList);
        List<Dict> roomList = studentMapper.roomList();
        model.addAttribute("roomList",roomList);
        List<Dict> apartmentList = studentMapper.apartmentList();
        model.addAttribute("apartmentList",apartmentList);

        return "add";
    }


    @RequestMapping("/add.do")
    public String add(@Valid StudentAdd studentAdd , BindingResult result,Model model){

        if (result.hasErrors()){
            model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            //1. 渲染3个下拉框
            List<Dict> studentList = studentMapper.studentList();
            model.addAttribute("studentList",studentList);
            List<Dict> roomList = studentMapper.roomList();
            model.addAttribute("roomList",roomList);
            List<Dict> apartmentList = studentMapper.apartmentList();
            model.addAttribute("apartmentList",apartmentList);
            return "add";
        }
        studentService.add(studentAdd);
        // 添加成功之后发送短信
        ZzyUtils.sendSms("17611221735","入住成功",5);

        return "redirect:list.do";


    }
}
